package com.ai.chatai.controller;

import com.ai.chatai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequiredArgsConstructor
@RequestMapping("/ai")
public class CustomerServiceController {
    private final ChatClient serviceChatClient;
    private final ChatHistoryRepository chatHistoryRepository;
    @RequestMapping(value = "/service",produces = "text/html;charset=utf-8")
    public Flux<String> chat(String prompt, String chatId) {
        //保存会话id
        chatHistoryRepository.save("service",chatId);
        //请求模型
        return serviceChatClient.prompt()
                .user(prompt)//设置用户输入
                .advisors(a->a.param(ChatMemory.CONVERSATION_ID,chatId))//设置会话id
                .stream()//流式回答
                .content();//获取内容
    }
}
